package com.rickpan.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

/**
 * 团队活动日志实体类
 * 
 * @author RickPan Team
 * @since 2024-01-15
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "team_activity_logs", indexes = {
    @Index(name = "idx_team_id", columnList = "team_id"),
    @Index(name = "idx_user_id", columnList = "user_id"),
    @Index(name = "idx_action_type", columnList = "action_type"),
    @Index(name = "idx_target_type_id", columnList = "target_type, target_id"),
    @Index(name = "idx_created_at", columnList = "created_at")
})
public class TeamActivityLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 所属团队ID
     */
    @Column(name = "team_id", nullable = false)
    private Long teamId;

    /**
     * 操作用户ID
     */
    @Column(name = "user_id", nullable = false)
    private Long userId;

    /**
     * 操作类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "action_type", nullable = false, length = 50)
    private ActionType actionType;

    /**
     * 目标对象类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "target_type", nullable = false, length = 50)
    private TargetType targetType;

    /**
     * 目标对象ID
     */
    @Column(name = "target_id", nullable = false)
    private Long targetId;

    /**
     * 目标对象标题
     */
    @Column(name = "target_title", length = 200)
    private String targetTitle;

    /**
     * 操作描述
     */
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 额外元数据(JSON格式)
     */
    @Column(name = "metadata", columnDefinition = "JSON")
    private String metadata;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    /**
     * 操作类型枚举
     */
    public enum ActionType {
        // 项目相关操作
        PROJECT_CREATE("创建项目"),
        PROJECT_UPDATE("更新项目"),
        PROJECT_DELETE("删除项目"),
        PROJECT_STATUS_CHANGE("项目状态变更"),
        PROJECT_MANAGER_ASSIGN("分配项目经理"),
        
        // 任务相关操作
        TASK_CREATE("创建任务"),
        TASK_UPDATE("更新任务"),
        TASK_DELETE("删除任务"),
        TASK_STATUS_CHANGE("任务状态变更"),
        TASK_ASSIGN("分配任务"),
        TASK_COMPLETE("完成任务"),
        
        // 公告相关操作
        ANNOUNCEMENT_CREATE("创建公告"),
        ANNOUNCEMENT_UPDATE("更新公告"),
        ANNOUNCEMENT_DELETE("删除公告"),
        ANNOUNCEMENT_PIN("置顶公告"),
        ANNOUNCEMENT_UNPIN("取消置顶公告"),
        
        // 团队相关操作
        TEAM_MEMBER_JOIN("成员加入"),
        TEAM_MEMBER_LEAVE("成员离开"),
        TEAM_MEMBER_ROLE_CHANGE("成员角色变更"),
        
        // 其他操作
        COMMENT_CREATE("创建评论"),
        COMMENT_DELETE("删除评论"),
        FILE_UPLOAD("上传文件"),
        FILE_DELETE("删除文件");

        private final String description;

        ActionType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 目标对象类型枚举
     */
    public enum TargetType {
        PROJECT("项目"),
        TASK("任务"),
        ANNOUNCEMENT("公告"),
        TEAM("团队"),
        USER("用户"),
        FILE("文件"),
        COMMENT("评论");

        private final String description;

        TargetType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 关联团队实体（多对一）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "team_id", insertable = false, updatable = false)
    private Team team;

    /**
     * 关联用户实体（多对一）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    private User user;

    /**
     * 构造函数
     */
    public TeamActivityLog() {}

    /**
     * 构造函数
     */
    public TeamActivityLog(Long teamId, Long userId, ActionType actionType, 
                          TargetType targetType, Long targetId, String targetTitle, String description) {
        this.teamId = teamId;
        this.userId = userId;
        this.actionType = actionType;
        this.targetType = targetType;
        this.targetId = targetId;
        this.targetTitle = targetTitle;
        this.description = description;
    }

    /**
     * 获取操作类型描述
     */
    public String getActionTypeDescription() {
        return actionType != null ? actionType.getDescription() : "";
    }

    /**
     * 获取目标类型描述
     */
    public String getTargetTypeDescription() {
        return targetType != null ? targetType.getDescription() : "";
    }

    /**
     * 生成活动描述
     */
    public String generateActivityDescription() {
        if (description != null && !description.trim().isEmpty()) {
            return description;
        }
        
        StringBuilder sb = new StringBuilder();
        sb.append(getActionTypeDescription());
        
        if (targetTitle != null && !targetTitle.trim().isEmpty()) {
            sb.append(": ").append(targetTitle);
        }
        
        return sb.toString();
    }

    /**
     * 检查是否是项目相关活动
     */
    public boolean isProjectActivity() {
        return TargetType.PROJECT.equals(targetType) || 
               actionType.name().startsWith("PROJECT_");
    }

    /**
     * 检查是否是任务相关活动
     */
    public boolean isTaskActivity() {
        return TargetType.TASK.equals(targetType) || 
               actionType.name().startsWith("TASK_");
    }

    /**
     * 检查是否是公告相关活动
     */
    public boolean isAnnouncementActivity() {
        return TargetType.ANNOUNCEMENT.equals(targetType) || 
               actionType.name().startsWith("ANNOUNCEMENT_");
    }

    /**
     * 检查是否是团队相关活动
     */
    public boolean isTeamActivity() {
        return TargetType.TEAM.equals(targetType) || 
               actionType.name().startsWith("TEAM_");
    }
}
